﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

using CPPEI.Coolzon.Common;
using System.Xml;
namespace CPPEI.Coolzon.Configuration.Client
{
    public class Config
    {
        public static HostConfig Host { get; private set; }
        public static List<HostConfig> SpareHostList { get; private set; }
        public static PartLibsConfig PartLibs { get; private set; }

        /// <summary>
        /// 初始化服务的配置信息
        /// </summary>
        /// <param name="configFile"></param>
        public static void InitHostConfig(string configFile)
        {
            try
            {
                XDocument xConfig = XDocument.Load(configFile);

                XElement xHost = xConfig.Element("Host");

                List<ServiceConfig> scList = (from a in xHost.Descendants("Service")
                                              where a != null
                                              select new ServiceConfig
                                              {
                                                  Name = a.GetXAttributeValue("Name"),
                                                  Path = a.GetXAttributeValue("Path"),
                                                  Port = Convert.ToInt32(a.GetXAttributeValue("Port")),
                                                  IP = a.GetXAttributeValue("IP")
                                              }).ToList<ServiceConfig>();
                Host = new HostConfig();
                Host.ServiceList = scList;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 初始化备用服务器信息
        /// </summary>
        /// <param name="configFile"></param>
        public static void InitSpareHostConfig(string configFile)
        {
            SpareHostList = new List<HostConfig>();

            try
            {
                XmlDocument xmlDoc = new XmlDocument();

                xmlDoc.Load(configFile);
                XmlNodeList xmlHostList = xmlDoc.SelectNodes(".//Spare-Host/Host");
                for (int index = 0; index < xmlHostList.Count; index++)
                {
                    XmlNode xmlHost = xmlHostList[index];
                    XmlNodeList xmlServiceList = xmlHost.SelectNodes("./Service");

                    HostConfig hostConfig = new HostConfig();
                    for (int iService = 0; iService < xmlServiceList.Count; iService++)
                    {
                        XmlNode xmlService = xmlServiceList[iService];
                        ServiceConfig serviceConfig = new ServiceConfig();
                        serviceConfig.Name = xmlService.Attributes["Name"].Value;
                        if (xmlService.Attributes["Path"] != null)
                        {
                            serviceConfig.Path = xmlService.Attributes["Path"].Value;
                        }
                        serviceConfig.Port = Convert.ToInt32(xmlService.Attributes["Port"].Value);
                        serviceConfig.IP = xmlService.Attributes["IP"].Value;
                        hostConfig.ServiceList.Add(serviceConfig);
                    }
                    SpareHostList.Add(hostConfig);
                }
            }
            catch (Exception ex)
            {
                Common.Logger.ErrorToTag(LogTag.Tag,"Config InitSpareHostConfig Error:{0}", ex);
            }

        }

        /// <summary>
        /// 初始化组件库配置信息
        /// </summary>
        /// <param name="configFile"></param>
        public static void InitPartLibsConfig(string configFile)
        {
            try
            {
                XDocument xConfig = XDocument.Load(configFile);

                XElement xPartLibs = xConfig.Element("PartLibs");

                PartLibs = new PartLibsConfig();
                PartLibs.PartLibsDirectory = xPartLibs.Attribute("Directory").Value;

                List<PartLib> partList = (from a in xPartLibs.Descendants("PartLib")
                                              where a != null
                                        select new PartLib
                                              {
                                                  Name = a.GetXAttributeValue("Name"),
                                                  Caption = a.GetXAttributeValue("Caption"),
                                                  Version = new Version(a.GetXAttributeValue("Version"))
                                              }).ToList<PartLib>();
                PartLibs.PartLibs = partList;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
